//Latte forum 
var crypto = require('crypto');
var config =require('./config')

module.exports = {
    isInteger:function(v){
         return Number.isInteger(v)
    },
    bad:function(res,m=''){
        if(res==null){ throw "Tools Error: you forgot to input the 'res' "};
        res.end('Latte: Bad request:'+m)
    },
    failed:function(res,m=''){
        if(res==null){ throw "Tools Error: you forgot to input the 'res' "};
        res.end('Latte: Failed:'+m)
    },
    ok:function(res,redirect_url,m=''){
        if(res==null){ throw "Tools Error: you forgot to input the 'res' "};
        //res.end('Latte: OK:'+m)
        res.redirect(redirect_url)
    },
    mustLogin:function(req,res,m=''){
        if(res==null || req==null ){ throw "Tools Error: you forgot to input the 'req' and 'res' "};
        if(req.session){
            if(req.session.beenLogin){
                return true
            }else{
               res.end('Latte: Need login: '+m);
               return false
            }
        }else{
            res.end('Latte: this server need Cookie function');
            return false
        }
    },
    SAFE_STRING: function(params){
        for(var i in params){
            if(typeof(params[i]) != 'string'){ 
                throw new Error(i+'is not String type');break
            }else{
                // params[i] = params[i].replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/&/g,"&amp;");
                if(i =='pwd'){
                    params[i] = crypto.createHash('md5').update(params[i]).digest('hex')
                }
            }
        };
        return params
    },
    /**base hte Ejs auto trans,only need hanlde parmas['content'] of POST */
    //2019-7-5留有一个bug，当用户上传图片时候，内容长度是会被剪切的，如果图片符号[[没有结尾，那么就是<img 标签没有闭合，
    //在帖子展示区域，首页缩略区域都可能导致视图bug
    SAFE_CONTENT:function(p_p){
        if(p_p['content']){
            var t = p_p['content'];
            var t1 = t.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/&/g,"&amp;");
            var t2 = t1.replace(/\r\n/g,'</p><p>').replace(/\[\[\s*/g,'</p><p><img src="').replace(/\s*\]\]/g,'" alt="user pic" style="max-width:800px;/></p><p>');
            var t3 =t2.replace(config.host,'none').replace('192.168','none');
            //t3用户外链图片存在安全隐患，这里对两个可能的本域进行过滤
            p_p['content'] = '<p>'+t3+'</p>';
        }else{
            throw new Error("bad paramter,this func only used for up post['content']")
        }
    },
    resubmit:function(marked,ms){
        if(marked){
            return (new Date - marked)<ms
        }else{
            return false
        }
    },
    md5:function(text) {
        return crypto.createHash('md5').update(text).digest('hex')
    },
    antiCrush:{
        ipList:{},
        actionCounter:function(req){
           var ip = req.ip;
           var ip_list = this.ipList;
           if(ip_list[ip]){
                ip_list[ip].n += 1
           }else{
               ip_list[ip]= {n:0,d:null};
               ip_list[ip].n = 1
           }
        },
        checkAction:function(req,max,long){
            var ip_list = this.ipList;
            if(ip_list[req.ip] && ip_list[req.ip].n > max){
                if(ip_list[req.ip].d == null){
                    ip_list[req.ip].d = new Date;
                    return long
                }else{
                    var now =new Date;
                    if(now - ip_list[req.ip].d < long){
                        return (now - ip_list[req.ip].d)           
                    }else{
                        delete ip_list[req.ip];  //clear marked
                        return false
                    }
                }
            }else{
                return false
            }
        },
    },

}